// AJAX INIT
function $$$(id) {
	return document.getElementById(id);
}
function khoitao_ajax()
{
	var x;
	try 
	{
		x	=	new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
    	try 
		{
			x	=	new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(f) { x	=	null; }
  	}
	if	((!x)&&(typeof XMLHttpRequest!="undefined"))
	{
		x=new XMLHttpRequest();
  	}
	return  x;
}
$.extend({
  ajaxIt : function(query, lnk, callbackFnk){    
	var http 	=	khoitao_ajax();
	try
	{
	  http.open("POST", lnk);
	  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  http.setRequestHeader("Cache-control", "no-cache");		
	  http.onreadystatechange = function()
	  {
		  if (http.readyState == 4)
		  {
			  if (http.status == 200)
			  {
				  $(".jGrowl-notification .jGrowl-message").each(function(i,e){
						if ($(this).text() == "Processing...")
						{
							$(this).parent().hide();
						}
					});
				  // Callback
				  if(typeof callbackFnk == 'function'){
					  x = http.responseText;
					  callbackFnk.call(this, x);
				  }
				  //---------
			   }
				else
				{
					alert('Có lỗi hệ thống. Xin vui lòng thử lại sau');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
  }
});
function	Forward(url)
{
	window.location.href = url;
}
function	_postback()
{
	return void(1);
}

jQuery(window).load(function(){ 
	Cufon.replace('.cufon', {fontFamily:'utm', hover:true});
});

function send_contact(contactfrm)
{
	$$$("notify").innerHTML = '<img src="/images/ajax-loader.gif" align="absmiddle" border="0" /> Thông tin của bạn đang được gởi đi.';
	
	var ten = contactfrm.txt_ten.value;
	
	var email = contactfrm.txt_email.value;
	
	var tieu_de = contactfrm.txt_tieu_de.value;
	
	var dia_chi = contactfrm.txtAddress.value;
	
	var dien_thoai = contactfrm.txtTel.value;
	
	var noi_dung = contactfrm.txt_noi_dung.value;
	
	if (!ten || ten.length < 8)
	{
		document.contactfrm.txt_ten.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Tên từ 8 ký tự trở lên</span> !';
		return false;
	}
	
	else if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/))
	{
		document.contactfrm.txt_email.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Email không đúng</span> !';
		return false;
	}	
	else if (!tieu_de || tieu_de.length < 8)
	{
		document.contactfrm.txt_tieu_de.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Tiêu đề quá ngắn</span> !';
		return false;
	}	
	else if (!noi_dung || noi_dung.length < 16)
	{
		document.contactfrm.txt_noi_dung.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Nội dung quá ngắn</span> !';
		return false;
	}		
	else
	{
	}

	var	query	=	"txt_ten="+ten+"&txt_tieu_de="+tieu_de+"&txt_noi_dung="+noi_dung+"&txt_email="+email+"&txtAddress="+dia_chi+'&txtTel='+dien_thoai;
	var http 	=	khoitao_ajax();
	try
	{
		http.open("POST", "/web/frontend.php/sendMail");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					x = $.trim(x);
					if (x != "OK")
					{
						$$$("notify").innerHTML = '<span style="color:#C00">'+x+'</span> !';
					}
					else
					{
						$$$("notify").innerHTML = '<span style="color:#C00">Thông tin của bạn đã được gửi đi</span> !';
					}
				}
				else
				{
					$$$("notify").innerHTML = '<span style="color:#C00">Có lỗi hệ thống. Xin vui lòng thử lại sau</span> !';
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
		$$$("notify").innerHTML = '<span style="color:#C00">Đang có lỗi hệ thống!</span><br>Vui lòng thử lại sau !';
	}
	return false;
	
}

function show_notify(msg)
{
	$.ajaxIt("act=show_thong_bao&msg="+encodeURIComponent(msg),'/action.php',function(x)
	{
		$("#luu_result").html(x);
	});
}

$(document).ready(function()
{
	$("a[rel=example_group]").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});

	// HOVER OF GROUND ITEM - HOME
	$(".ground_item").hover(function()
	{
		$(this).find('.ground_content:first').slideDown(200);
	},function()
	{
		$(this).find('.ground_content:first').slideUp();
	});
});

function scroll_to_content()
{
	$('html, body').animate({scrollTop: '750px'}, 500);
}

$(window).load(function()
{
	var t = setInterval(function()
	{
		var active = $(".slide_menu").find('.active:first');
		
		if (active.next().hasClass('slide_menu_item'))
		{
			active.next().addClass('active');
			main_slide = active.next();
		}
		else
		{
			$(".slide_menu_item:first").addClass('active');
			main_slide = $(".slide_menu_item:first");
		}
		
		main_slide_url = main_slide.find('img:first').attr('src');
		main_slide_url = main_slide_url.substring(13);
		
		$(".main_img").find('img:first').css('opacity','0.5');
		$(".main_img").find('img:first').attr('src','/uploads/cms/big_'+main_slide_url).css('opacity','1');
		
		active.removeClass('active');
	},5000);
	
	$(".slide_menu_item").click(function()
	{
		if (!$(this).hasClass('active'))
		{
			main_slide_url = $(this).find('img:first').attr('src');
			main_slide_url = main_slide_url.substring(13);
			
			$(".main_img").find('img:first').css('opacity','0.5');
			$(".main_img").find('img:first').attr('src','/uploads/cms/big_'+main_slide_url).css('opacity','1');
			$('.slide_menu_item').removeClass('active');
			$(this).addClass('active');
		}
	});
});

function showLoader() {
	$("#loading_result").html("<div class=\"loading_bao\" style=\"position:fixed; top:50%; right:50%; text-align:center; width:120px;  color:#000000;background-color: rgba(57, 83, 83, 0.7);z-index:99999999999; padding: 8px;-moz-border-radius: 6px;border-radius: 6px;\"><div  style=\"background:#fff; height:50px;padding-top: 15px; \" >Đang tải...<br><img src='/images/ajax-loader1.gif'></div></div>").hide().fadeIn(10);
	block = true;
}
function closeLoader() {
	$("#loading_result").html("").hide().fadeOut(100);
	block = false;
}
function showResult(type,data) {
	closeLoader();
	$("#"+type+"").html(data).hide().fadeIn(100);
	block = false;
}

function show_popup(ground_id)
{
	$.ajax({
		url:'/web/frontend.php/ajax/show_popup.html',
        type: 'POST',
        data: 'ground_id=' + ground_id,
        dataType: "html",
        success: function(data){
			showResult("show_thong_tin",data);
        }
    });
	return false;
}